Skip to content

feat!: anonymous usage aggregates for admins - #571

Open
uniqueg wants to merge 9 commits into
devfrom
feat/569-anonymous-usage
Open

uniqueg wants to merge 9 commits into
devfrom
feat/569-anonymous-usage

Conversation

@uniqueg

@uniqueg uniqueg commented Dec 5, 2026 •

Copy link
Copy Markdown
Member

Stacked on #570. This branch contains #570's commits. The change of this PR is the last commit, feat!: anonymous usage aggregates for admins. The PR targets dev so that CI runs; CI runs only on PRs into dev. After #570 is merged, update this branch from dev and the diff shrinks to this change.

Summary

GET /usage no longer gives admins the totals of single users (D-555):

  • Own totals for every caller:
    • The totals of the tasks the caller owns, then, per service client, the totals of the tasks that client ran for the caller (acting_for).
    • Service clients split their own totals by named user with group_by=acting_for.
    • There is no minimum for one's own totals.
  • Anonymous totals for admins:
    • aggregate sums all tasks in the time range (since, until), without user identifiers.
    • If it covers fewer tasks than the new setting custom.accounting.min_aggregate_tasks (default 5, at least 1), it holds only {"withheld": true}.
    • Otherwise it holds {"withheld": false, "tasks": ..., ...}.
  • Filters: user_id and acting_for may only name the caller. Naming anyone else gives 403 Forbidden, also for admins. With user_id, only the caller's own tasks are counted; with acting_for, only the tasks run for the caller.
  • Rules: admins-get-usage is renamed to authenticated-get-usage ("*"), and the controller decides what each caller gets. Overlays that name the old rule stop the start: a value is refused by proTES (RENAMED_RULES), and null by FOCA.
  • GET /usage/reports (execution reports) is unchanged and admin-only.
  • Docs:
    • OpenAPI: GetUsage and the new AggregateUsage schema.
    • docs/api.md: the table and the Usage section.
    • docs/access-control.md, docs/operations.md and the comments in config.yaml and values.yaml.
    • docs/configuration.md: the new setting.
    • docs/upgrading.md: a breaking-change note, and the older sections annotated.

Deviations from the issue text (Claude's proposal; D-555's goal holds)

  • The issue says admins with user_id, acting_for or group_by get 400. Here every caller, admins included, gets their own totals, and the same rules apply to everyone: naming anyone else gives 403, and group_by applies to one's own rows. This matters for admins who also own tasks, e.g., the role admin together with user. They get both their own rows and the anonymous totals, and the API has one consistent rule instead of a special case per role.
  • Withheld anonymous totals hold only "withheld": true, not the number of tasks, so that small counts aren't revealed either.

Verification

  • Tests first:
    • The endpoint tests in tests/accounting/test_usage_endpoint.py were written before the controller change. Run against the old controller, they failed: admins got per-user rows, users got 403, and there was no aggregate.
    • The review rounds added unit tests of own_usage_totals and aggregate_totals in tests/accounting/test_report.py: all flag combinations, no double counting, threshold boundary 5 and 6, time range, empty collection, float overflow.
    • More endpoint tests followed: HEAD, the aggregate independent of group_by, the 403 detail, and the exact error for each form of the old rule in an overlay.
  • pre-commit run --all-files: all hooks pass (ruff, mypy, semgrep, actionlint, gitleaks, ...).
  • coverage run -m pytest -W ignore::DeprecationWarning (mongomock) at 07dae3c: 2664 passed, 11 skipped, coverage 100%. The later commits change only docs, comments and one test assertion; tests/accounting/test_report.py passes.
  • The tests against a real MongoDB didn't run locally; CI runs them.
  • CI at 3a4d03c: all test, lint, docs, chart, smoke and integration jobs pass. Scan image and Scan release image fail on OpenSSL CVEs in the FOCA base image. fix!: provenance only for the task owner #570 and dev have the same failures; they are unrelated to this change and block the CI passed gate. CI at 48fd6b2: same result, all other jobs pass (Test mongomock and MongoDB 7.0, Integration, Smoke test, Chart, Lint, Docs, CodeQL).

Reviews

  • Adversarial, quality and contract reviews, two rounds.

  • Fixed: unit tests for the new report functions; one shared _sums() accumulator helper; flags named include_owned and include_acted_for; precise exceptions per overlay case of the old rule.

  • Docs, also fixed:

    • they now say that the minimum counts tasks, not users, and doesn't stop differencing over overlapping time ranges;
    • they warn that min_aggregate_tasks: 1 gives single-task totals;
    • user_id takes the stored, namespaced form;
    • the 0.8.0 upgrade note names the renamed rule;
    • stale "per user" wording is gone.
  • Sourcery (6 threads): fixed group_by validated before either totals branch, the acting_for description in the spec, two tighter tests (48fd6b2); answered: the deviation for admins (below) and rolling upgrades (already in the upgrade note). All threads resolved.

Declined review findings

  • Count distinct users instead of tasks for the minimum (medium): D-555 says "fewer than a configurable number of tasks", so the code counts tasks. The docs now state the limits plainly. A distinct-user minimum would be a design change for uniqueg.
  • Coarser since/until against differencing (medium): this is inherent to arbitrary time ranges, and the issue doesn't ask for it. Documented as not prevented.
  • 400 for user_id and acting_for both naming the caller (low): documented as matching no tasks, and it leaks nothing.
  • Annotate docs/releases/0.8.0.md (low): these are the notes of a released version; docs/upgrading.md carries the pointers.
  • A 401 response for /usage in the spec (low): the gap predates this PR, and no other proTES extension path in additions.openapi.yaml lists a 401 either.

Merge gate

#570 merges first; then this branch is updated from dev. The Scan image failure (base-image OpenSSL CVEs) must be fixed or excepted for the CI passed gate; that is separate work.

Open questions

None. The deviations above are Claude's proposal; D-555's goal holds.

Closes #569

🤖 Generated with Claude Code

https://claude.ai/code/session_011FSoqPsbB5s4NvhhYYcDJk

uniqueg and others added 5 commits December 5, 2026 22:40
Admins no longer get the provenance of tasks they don't own: the rule
`admins-get-provenance` is removed from the base config, so that
`GET /tasks/{id}/provenance` answers only to the task's owner. Everyone
else, admins included, gets `403 Forbidden`, also for unknown tasks, as
for `GET /tasks/{id}`. Admins of a TES gateway must not see the details
of other users' tasks.

Tests cover the owner (200), other users, outsiders and admins (403 for
existing and unknown tasks, GET and HEAD), no token (401), the base
config's rules, and overlays that still remove the rule (startup stops
with FOCA's error). Docs (API, access control, operations, OpenAPI) and
the upgrade notes are updated.

BREAKING CHANGE: admins get `403` for the provenance of tasks they don't
own; overlays with `admins-get-provenance: null` must drop that entry.

Closes #568

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011FSoqPsbB5s4NvhhYYcDJk
Tests that the rule stored by earlier versions stops applying when the
app starts without it, and that variants of the provenance path never
give admins the provenance. The upgrade notes name custom lists of
rules, which must drop the rule too; docs re-wrapped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011FSoqPsbB5s4NvhhYYcDJk
The 0.4.0 upgrade checklist no longer tells custom rule lists to add
`admins-get-provenance`, but to leave it out; the provenance docs say
that requests without a valid token get `401`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011FSoqPsbB5s4NvhhYYcDJk
The Permissions API section still listed the provenance of tasks among
what admins see; it is for the task's owner only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011FSoqPsbB5s4NvhhYYcDJk
`GET /usage` no longer gives admins the totals of single users. Every
authenticated caller gets the totals of their own tasks and of the
tasks that service clients ran for them; service clients split theirs
by named user (`group_by=acting_for`). Admins also get anonymous totals
over all tasks (`aggregate`), withheld (`"withheld": true`) below
`custom.accounting.min_aggregate_tasks` tasks (default 5, at least 1).
`user_id` and `acting_for` may only name the caller (403 otherwise,
also for admins). Execution reports are unchanged.

The rule `admins-get-usage` is renamed to `authenticated-get-usage`;
overlays that name the old rule stop the start.

BREAKING CHANGE: admins get only anonymous totals at `GET /usage`;
the rule `admins-get-usage` is renamed to `authenticated-get-usage`.

Closes #569

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011FSoqPsbB5s4NvhhYYcDJk
@sourcery-ai

sourcery-ai Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Data structures

Add the configurable minimum task count that determines when anonymous usage aggregates are withheld.

  • pro_tes/accounting/config.py L265-267, L272-273, L280
  • pro_tes/config.yaml L444-446, L451-453

API

Change the usage endpoint contract to return caller-owned totals and an admin-only anonymous aggregate, and document its filters and response schema.

  • pro_tes/api/additions.openapi.yaml L96-102, L251-291, L316-320 (+10 small edits)

Restrict provenance access to task owners and replace the admin-only usage policy with an authenticated usage policy.

  • pro_tes/config.yaml L64-69, L100-105, L441
  • pro_tes/security.py L55

Uses of the API

Have the usage controller enforce caller-only filters, return the caller’s totals, and include thresholded aggregates for admins.

  • pro_tes/ga4gh/tes/server.py L18-30, L137-190

Implementation

Implement caller-owned usage totals and anonymous aggregate totals using shared aggregation fields, including withholding below the configured threshold.

  • pro_tes/accounting/__init__.py L6-9, L37-41, L46-50 (+3 small edits)
  • pro_tes/accounting/report.py L39-55, L133-234 (+5 small edits)

Define the forbidden response for requests that try to retrieve another caller’s usage.

  • pro_tes/exceptions.py L173-180, L258-265

Update provenance method documentation to reflect the owner-only access policy.

  • pro_tes/ga4gh/tes/task_runs.py L817-818

Configuration

Update the deployment example’s accounting comments to describe owner totals and anonymous admin totals.

  • deployment/values.yaml L68-69

Documentation

Document the revised roles, caller-only usage totals and filters, anonymous aggregate threshold, and owner-only provenance.

  • docs/access-control.md L61-66, L101-107, L399-407 (+2 small edits)
  • docs/api.md L182-199, L216-248, L270-285 (+3 small edits)
  • docs/configuration.md L821-823, L880-895
  • docs/operations.md L322-324, L426

Record the breaking changes, renamed usage rule, new aggregate setting, and effects on upgrades and rolling deployments.

  • docs/upgrading.md L18-65, L922-927 (+4 small edits)

Tests

Test owner-only provenance access and the removal of the former admin provenance rule, including upgrade and path variants.

  • tests/ga4gh/tes/test_provenance_api.py L88-116, L267-295, L329-357 (+7 small edits)
  • tests/retention/test_retention_endpoint.py L128-131

Test caller usage totals, filter authorization, anonymous aggregate thresholds and ranges, and the renamed access-control rule.

  • tests/accounting/test_report.py L1, L12-17, L214-346
  • tests/accounting/test_usage_endpoint.py L33-90, L95-351, L412-449 (+7 small edits)
  • tests/ga4gh/tes/test_service_clients.py L424

Other changes

Files not grouped.

  • pro_tes/access_control/model.conf L7-8

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@read-the-docs-community

read-the-docs-community Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

📖 6 files modified


Preview · build overview · proTES · built Dec. 6, 2026, 12:23 a.m. UTC

@codecov

codecov Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (e1e0683) to head (48fd6b2).

Additional details and impacted files
@@            Coverage Diff            @@
##               dev      #571   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           39        39           
  Lines         4021      4057   +36     
=========================================
+ Hits          4021      4057   +36     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

uniqueg and others added 3 commits December 6, 2026 00:27
Review fixes for #569: `own_usage_totals` and `aggregate_totals` share
the accumulators of `usage_totals` and get unit tests; the flags are
named `include_owned` and `include_acted_for`. The docs no longer claim
more than the minimum gives: it counts tasks, not users, and doesn't
stop differencing. `user_id` takes the stored (namespaced) form. More
endpoint tests (HEAD, grouping of aggregates, error detail, the error
of each overlay of the old rule); comments re-wrapped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011FSoqPsbB5s4NvhhYYcDJk
Second-round review fixes for #569: module and OpenAPI comments no
longer say "per user"; the docs say that `min_aggregate_tasks: 1`
gives single-task totals; the include flags test checks `acting_for`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011FSoqPsbB5s4NvhhYYcDJk
The 0.8.0 upgrade section now says that `admins-get-usage` is named
`authenticated-get-usage` since the unreleased changes, and that the
old name stops the start (contract review of #569).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011FSoqPsbB5s4NvhhYYcDJk
@uniqueg
uniqueg marked this pull request as ready for review December 5, 2026 23:58
@sourcery-ai

sourcery-ai Bot commented Dec 6, 2026 •

Copy link
Copy Markdown

Linked issues

Checked at commit 48fd6b2

#569 feat!: anonymous usage aggregates for admins · 6 of 8 met · 1 to check by hand

# Requirement Status Notes
1 Give admins anonymous totals over all tasks in the requested time range, without other users' totals. ✅ Met The aggregate is calculated across the time range and contains no user identifiers.
2 Withhold aggregates below configurable min_aggregate_tasks (default 5, minimum 1), returning only the withheld flag. ✅ Met The setting defaults to 5, validates a minimum of 1, and below-threshold or empty aggregates contain only withheld: true.
3 Give every caller their own totals, including service-client tasks run for them; let service clients split their totals by acting_for. ✅ Met Own totals are returned before client-run-for-caller totals; service clients can group their own totals by acting_for.
4 Reject filters naming someone other than the caller with 403 Forbidden. ✅ Met The controller applies this check to both user_id and acting_for, including for admins.
5 For admins, reject user_id, acting_for, or group_by with 400 Bad Request. 🟡 Partly met There is no admin-specific rejection: own user_id or acting_for filters and group_by are accepted, while filters naming others return 403; the endpoint tests encode this behavior instead of the requested 400.
6 Keep GET /usage/reports unchanged and admin-only. ✅ Met The reports endpoint remains admin-only.
7 Update the OpenAPI description, API and configuration documentation, and upgrade notes. ✅ Met The OpenAPI spec and the requested documentation sections describe the aggregate, setting, and breaking changes.
8 Release the change with the next proTES release. 👀 Check by hand (external system) Confirm that the change is included in the next proTES release.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 6 issues

Sourcery assessment

Needs a human reviewer. 5 findings to address first, and every authenticated caller can now query usage, with the controller relying on get_owner() and the user_id/acting_for checks to keep results scoped to that caller. If that identity scoping is wrong, another caller's usage is disclosed; reverting stops future access but cannot undo data already returned.

Blocking findings: pro_tes/ga4gh/tes/server.py:174, pro_tes/config.yaml:105, pro_tes/accounting/report.py:176, tests/accounting/test_usage_endpoint.py:287, tests/accounting/test_usage_endpoint.py:343


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread pro_tes/ga4gh/tes/server.py Resolved
Comment thread pro_tes/config.yaml Resolved
Comment thread pro_tes/accounting/report.py Resolved
Comment thread pro_tes/accounting/report.py Resolved
Comment thread tests/accounting/test_usage_endpoint.py Resolved
Comment thread tests/accounting/test_usage_endpoint.py Resolved
Sourcery review of #571: `own_usage_totals` refuses unknown groupings
also when it leaves out the caller's own tasks; the spec says where
`acting_for` appears; the tests of withheld totals and of service
clients' own rows check whole values.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011FSoqPsbB5s4NvhhYYcDJk

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat!: anonymous usage aggregates for admins

1 participant